home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


CHAPTER 4
Constants and Enums

Constants and enumerated types provide symbolic representation for numeric values. They let you use an understandable name for a value instead of a meaningless numeric quantity. Constants and enumerated types are very easy to use, so there is no excuse for using hard-coded numbers instead.

The following sections explain constants and enumerated types, and they discuss a few related topics that are not immediately obvious.

Use No Magic Numbers

When someone else reads your code later, hard-coded numbers like 12 and –32768 often seem mysterious. Because these numbers seem to have been picked out of thin air, they are sometimes called magic numbers.

In the following code, the InitializeColors routine and the declaration of the array BackgroundColors are in two different modules. If someone reads the InitializeColors code without having recently looked at the definition of the BackgroundColors array, the choice of the magic number 7 is puzzling.

Public BackgroundColors(0 To 7) As OLE_COLOR

In another module:

Private Sub InitializeColors()
 Dim i As Integer

     ‘ Fill in the BackgroundColors array’s entries.
      For i = 0 To 7
          BackgroundColors(i) = QBColor(i)
      Next i
End Sub

You can make the code clear by commenting every occurrence of the number 7.

Private Sub InitializeColors()
 Dim i As Integer

     ‘ Fill in the BackgroundColors array’s entries.
     ‘ They are indexed from 0 to 7.
     For i = 0 To 7
         BackgroundColors(i) = QBColor(i)
     Next i
End Sub

This helps, but it has a few drawbacks. It requires you to add a lot of comments to the code. It also makes it much harder to change the size of the array later. If you eventually decide the array should have bounds from 1 to 8, you need to search for every occurrence of the values 0 and 7 and change them to 1 and 8. You cannot simply perform an automatic search and replace because these values may be used for other things. If the program also contains a control array with controls indexed from 0 to 7, you do not want to replace occurrences of 7 that involve the control array.

Constants and enumerated values prevent all of these problems. Use meaningful names so the reader can tell what the constant represents. You can add extra comments where the values are defined to give the reader more detailed information.

Changing an array’s bounds is easy if they are defined using constants. You can simply change the constants’ definitions.

‘ We need 8 colors with indexes 0 through 7.
‘ At some point we will use the Mod operator to
‘ pick one.
Public Const MAX_COLOR = 7
Public BackgroundColors(0 To MAX_COLOR) As OLE_COLOR

‘ In another module:
Private Sub InitializeColors()
Dim i As Integer

    ‘ Fill in the BackgroundColors array’s entries.
     For i = 0 To MAX_COLOR
         BackgroundColors(i) = QBColor(i)
     Next i
End Sub

To change the BackgroundColors array so it has 16 elements, all you need to do is change the definition of MAX_COLOR.

‘ We need 16 colors with indexes 0 through 15.
Public Const MAX_COLOR = 15

Dimension Arrays with Constants

When you dimension arrays, it is important that you use constants or enumerated values instead of hard-coded numbers. This makes it easy for you to later change the array bounds.

It is particularly important if you have several arrays that must have the same bounds. By changing one constant definition, you can change all of the array bounds without worrying about verifying that all the array declarations are kept synchronized.

Public Const NUM_EMPLOYEES = 100

Public EmployeeNames(1 To NUM_EMPLOYEES) As String
Public EmployeeIds(1 To NUM_EMPLOYEES) As Long
Public EmployeePhoneNumbers(1 To NUM_EMPLOYEES) As String

When you resize an array dynamically, use a constant, enumerated value, or variable. For example:

Public NumEmployees As Integer
Public EmployeeNames() As String
    :

    NumEmployees = NumEmployees + 1
    ReDim Preserve Employees(1 To NumEmployees)

These examples use constants, enumerated values, or variables to determine upper bounds for arrays, but they still hard code the lower bounds as 1. Many programs hard code lower array bounds when they are 0 or 1. Usually this is not confusing. However, if the program contains many arrays that have lower bound 0 and many others with lower bound 1, you may want to create constants for these values as well.

‘ We need 8 colors with indexes 0 through 7.
 Public Const MIN_COLOR = 0
Public Const MAX_COLOR = 7
Public BackgroundColors(MIN_COLOR To MAX_COLOR) As OLE_COLOR

‘ In another module:
Private Sub InitializeColors()
Dim i As Integer

 ‘ Fill in the BackgroundColors array’s entries.
 For i = MIN_COLOR To MAX_COLOR
 BackgroundColors(i) = QBColor(i)
 Next i
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.